<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

if( $_REQUEST["agent_id"])
{

	$agent_id = strip_tags($_REQUEST["agent_id"]);

	$name_short = mysql_real_escape_string(strip_tags($_REQUEST["name_short"]));
    $name_full = mysql_real_escape_string(strip_tags($_REQUEST["name_full"]));
	//$name_short = GetSQLValueString(strip_tags($_REQUEST["name_short"]),"text");
    //$name_full = GetSQLValueString(strip_tags($_REQUEST["name_full"]), "text");
	$agent_type_id =  strip_tags($_REQUEST["agent_type_id"]);
    $date_start = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_start"])));
    $date_end = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_end"])));
    $modified = date ("Y-m-d H:m:s");    
	
	/*
    file_put_contents("flex POST.txt",$_REQUEST["name_short"].' \n 
    		After encoding '.$name_short. '\n  
    		MySql Real Escape ' .mysql_real_escape_string( $_REQUEST["name_short"]).'\n
    		utf8_decode ' .utf8_decode( $_REQUEST["name_short"]).'\n
    		utf8_encode ' .utf8_encode($_REQUEST["name_short"]));*/

	//Update core agent fields
	$db_query = sprintf("UPDATE agent 
							SET name_short='".$name_short."',
								name_full='".$name_full."',
								date_start='".$date_start."',
								date_end='".$date_end."',
								modified='".$modified."'
							WHERE agent_id='".$agent_id."'");
 
 	//file_put_contents  ( "update_agent.txt", $db_query);   
 	
 	$result = mysql_query($db_query);
    	if (!$result) {
        		die('invalid agent update query');    // . mysql_error());
    		}     
 	
	//mysql_free_result( $result );	    		
    
	if(($agent_type_id == 12))
 	{	    
    	$title_id = strip_tags($_REQUEST["title_id"]);
		$gender_id = strip_tags($_REQUEST["gender_id"]);
		$primary_role_id = strip_tags($_REQUEST["primary_role_id"]);
		$email = strip_tags($_REQUEST["email"]);
		$telephone = strip_tags($_REQUEST["telephone"]);
		$mobile = strip_tags($_REQUEST["mobile"]);
		$im_name = strip_tags($_REQUEST["im_name"]);
		$im_service_id = strip_tags($_REQUEST["im_service_id"]);
		$fax = strip_tags($_REQUEST["fax"]);
		$website = strip_tags($_REQUEST["website"]);
		
		$db_query = sprintf("UPDATE people 
							SET title_id='".$title_id."',
								gender_id='".$gender_id."',
								primary_role_id='".$primary_role_id."',
								email='".$email."',
								telephone='".$telephone."',
								mobile='".$mobile."',
								im_name='".$im_name."',
								im_service_id='".$im_service_id."',
								fax='".$fax."',
								website='".$website."'
							WHERE agent_person_id='".$agent_id."'");
 
 		//file_put_contents  ( "update_people.txt", $db_query);   
 
 		$resultSpecificAgent = mysql_query($db_query);
    		
 		if (!$resultSpecificAgent) {
        		die('invalid person update query');    // . mysql_error());
    		}		
 	}
 	elseif (($agent_type_id == 15))
 	{
	    $membership_type_id = strip_tags($_REQUEST["membership_type_id"]);
		$country_iso_code_3 = strip_tags($_REQUEST["country_iso_code_3"]);
		$country_iso_code_2 = strip_tags($_REQUEST["country_iso_code_2"]);
		$status_id = strip_tags($_REQUEST["status_id"]);
		$continent_id = strip_tags($_REQUEST["continent_id"]);
		$region_id = strip_tags($_REQUEST["region_id"]);
		$subregion_id = strip_tags($_REQUEST["subregion_id"]);
		$development_rating_id = strip_tags($_REQUEST["development_rating_id"]);
		$sign_mou_2001 = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["sign_mou_2001"])));
		$sign_mou_2007 = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["sign_mou_2007"])));
		$mou_signed_by_id = strip_tags($_REQUEST["mou_signed_by_id"]);
	 
		//Update Direct to database with SQL until Doctrine issue resolved
		$db_query = sprintf("UPDATE countries_participants 
							SET membership_type_id='".$membership_type_id."',
								country_iso_code_3='".$country_iso_code_3."',
								country_iso_code_2='".$country_iso_code_2."',
								status_id='".$status_id."',
								continent_id='".$continent_id."',
								region_id='".$region_id."',
								subregion_id='".$subregion_id."',
								development_rating_id='".$development_rating_id."',
								sign_mou_2001='".$sign_mou_2001."',
								sign_mou_2007='".$sign_mou_2007."',
								mou_signed_by_id='".$mou_signed_by_id."'
							WHERE agent_country_id='".$agent_id."'");
 
 		//file_put_contents  ( "update_country.txt", $db_query);   
 
 		$resultSpecificAgent = mysql_query($db_query);
    	if (!$resultSpecificAgent) {
        		die('invalid query');    // . mysql_error());
    		}	
 	}
 	elseif (($agent_type_id == 16))
 	{
 		// Update Event Data
	    $event_type_id = strip_tags($_REQUEST["event_type_id"]);
		$event_status_id = strip_tags($_REQUEST["event_status_id"]);
		$language_id = strip_tags($_REQUEST["language_id"]);
		$event_audience_id = strip_tags($_REQUEST["event_audience_id"]);
		 
		//Update Direct to database with SQL until Doctrine issue resolved
		$db_query = sprintf("UPDATE gbif_events 
								SET event_type_id='".$event_type_id."',
									event_status_id='".$event_status_id."',
									language_id='".$language_id."',
									event_audience_id='".$event_audience_id."'
								WHERE agent_event_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_events.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		}
 	}
 	elseif (($agent_type_id == 13))
 	{
 		//Project Agent
	    $type_id = strip_tags($_REQUEST["type_id"]);
		$status_id = strip_tags($_REQUEST["status_id"]);
		$reference_no = mysql_real_escape_string($_REQUEST["reference_no"]);
		$date_application = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_application"])));
		$date_awarded = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_awarded"])));
		$budget_awarded = strip_tags($_REQUEST["budget_awarded"]);
		$budget_released = strip_tags($_REQUEST["budget_released"]);
		
		//Update Direct to database with SQL until Doctrine issue resolved
		$db_query = sprintf("UPDATE projects 
								SET type_id='".$type_id."',
									status_id='".$status_id."',
									reference_no='".$reference_no."',
									date_application='".$date_application."',
									date_awarded='".$date_awarded."',
									budget_awarded='".$budget_awarded."',
									budget_released='".$budget_released."'
								WHERE agent_project_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_projects.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		}
 		
 	}
 	elseif (($agent_type_id == 14))
 	{
 		// Update Nodes
 	    $node_type_id = strip_tags($_REQUEST["node_type_id"]);
		$dev_status_id = strip_tags($_REQUEST["dev_status_id"]);
		
		$db_query = sprintf("UPDATE nodes 
								SET node_type_id='".$node_type_id."',
									dev_status_id='".$dev_status_id."'									
								WHERE agent_node_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_nodes.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		}
 	}
 	elseif (($agent_type_id == 18))
 	{
 		//Organisations / Participants
 	 	    $purpose_id = strip_tags($_REQUEST["purpose_id"]);
			$gbif_status_id = strip_tags($_REQUEST["gbif_status_id"]);
			$phone = mysql_real_escape_string($_REQUEST["phone"]);
			$fax = mysql_real_escape_string($_REQUEST["fax"]);
			$email = mysql_real_escape_string($_REQUEST["email"]);
			$website = mysql_real_escape_string($_REQUEST["website"]);
		
		$db_query = sprintf("UPDATE organisations 
								SET purpose_id='".$purpose_id."',
									gbif_status_id='".$gbif_status_id."',
									phone='".$phone."',
									fax='".$fax."',
									email='".$email."',
									website='".$website."'
								WHERE agent_org_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_organisations.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		} 	
 	} 	
 	elseif (($agent_type_id == 19))
 	{
 		//Suppliers
 	 	    $line_of_business_id = strip_tags($_REQUEST["line_of_business_id"]);
 	 	    $cvr_number = mysql_real_escape_string($_REQUEST["cvr_number"]);
			$phone = mysql_real_escape_string($_REQUEST["phone"]);
			$fax = mysql_real_escape_string($_REQUEST["fax"]);
			$email = mysql_real_escape_string($_REQUEST["email"]);
			$website = mysql_real_escape_string($_REQUEST["website"]);
		
		$db_query = sprintf("UPDATE suppliers 
								SET line_of_business_id='".$line_of_business_id."',
									cvr_number='".$cvr_number."',	
									phone='".$phone."',
									fax='".$fax."',
									email='".$email."',
									website='".$website."'
								WHERE agent_supplier_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_suppliers.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		} 	
 	} 	
	elseif (($agent_type_id == 225))
 	{
 		//Contract Agent
		$date_of_signature = strip_tags($_REQUEST["date_of_signature"]);
		$contract_status_id = strip_tags($_REQUEST["contract_status_id"]);
		$budget_final_amount = strip_tags($_REQUEST["budget_final_amount"]);
		$budget_released = strip_tags($_REQUEST["budget_released"]);
		
		//Update Direct to database with SQL until Doctrine issue resolved
		$db_query = sprintf("UPDATE contracts 
								SET date_of_signature='".$date_of_signature."',
									contract_status_id='".$contract_status_id."',
									budget_final_amount='".$budget_final_amount."',
									budget_released='".$budget_released."'
								WHERE agent_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_contracts.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		}
 		
 	} 	
	elseif (($agent_type_id == 28))
 	{
 		//Resource
		$resource_link = strip_tags($_REQUEST["resource_link"]);
		
		//Update Direct to database with SQL until Doctrine issue resolved
		$db_query = sprintf("UPDATE resources 
								SET resource_link='".$resource_link."'
								WHERE agent_id='".$agent_id."'");
	 
	 	//file_put_contents  ( "update_resources.txt", $db_query);   
	 
	 	$resultSpecificAgent = mysql_query($db_query);
	    	if (!$resultSpecificAgent) {
	        		die('invalid query');    // . mysql_error());
	    		}
 		
 	}
 	//28, 29 do not require extension to AGENT base table
 	
 	//mysql_free_result( $resultSpecificAgent );
    
 	$dom = new DomDocument('1.0');
	$dom->formatOutput = true;

	$root = $dom->createElement( "results" );
		$dom->appendChild( $root );
			$dn = $dom->createElement( "record" );
			$dn->setAttribute( 'updated_agent_id', $agent_id );
		$root->appendChild( $dn );	

	header( "Content-type: text/xml");
	echo $dom->saveXML();

	//Not sure whether to use this???
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? '"' . $theValue . '"' : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? '"' . doubleval($theValue) . '"' : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? '"' . $theValue . '"' : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
	
}  
?>